package com.ruoyi.web.controller.iot;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.modbus.utils.CacheShebeiUtils;
import com.ruoyi.common.modbus.utils.CacheUtils;
import com.ruoyi.system.config.IotModbusServerConfig;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.vo.IotDeviceVo;
import com.ruoyi.system.domain.vo.IotVariableVo;
import com.ruoyi.system.domain.vo.ValveParams;
import com.ruoyi.system.service.IIotDeviceService;
import com.ruoyi.system.service.IIotModbusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/iot/modbus")
public class IotModbusController {

    @Autowired
    private IotModbusServerConfig iotModbusServerConfig;

    @Autowired
    private IIotDeviceService iIotDeviceService;

    @Autowired
    private IIotModbusService iIotModbusService;

    @GetMapping("/distributeOrder")
    public AjaxResult distributeOrder()
    {
        iotModbusServerConfig.getMiiServer().sender().testIot("127.0.0.1",1);
        return AjaxResult.success(true);
    }

//    @Log(title = "关闭阀门", businessType = BusinessType.UPDATE)
    @PutMapping("/closeValve")
    public AjaxResult closeValve(@Validated @RequestBody ValveParams valveParams) {
        iotModbusServerConfig.getMiiServer().sender().closeValve(valveParams.getSn(), valveParams.getSlaveAddress());
        iotModbusServerConfig.getMiiServer().sender().testIot(valveParams.getSn(), valveParams.getSlaveAddress());
        return AjaxResult.success(true);
    }

//    @Log(title = "打开阀门", businessType = BusinessType.UPDATE)
    @PutMapping("/openValve")
    public AjaxResult openValve(@Validated @RequestBody ValveParams valveParams) {
        iotModbusServerConfig.getMiiServer().sender().openValve(valveParams.getSn(), valveParams.getSlaveAddress());
        iotModbusServerConfig.getMiiServer().sender().testIot(valveParams.getSn(), valveParams.getSlaveAddress());
        return AjaxResult.success(true);
    }

    @Autowired
    private IIotDeviceService iotDeviceService;

    @PutMapping("/udpateValve")
    public AjaxResult udpateValve(@Validated @RequestBody ValveParams valveParams) {
        IotDeviceVo iotDeviceVo = iIotDeviceService.selectIotDeviceByDeviceId(valveParams.getDeviceId());
        IotVariableVo iotVariableVo = iotDeviceService.selectVariableById(valveParams.getVariableId());
        if (valveParams.getVariableValue().equals("1")) {
            iotModbusServerConfig.getMiiServer().sender().openValve(iotVariableVo.getSerialNum()-1,iotDeviceVo.getSn(), iotDeviceVo.getSlaveAddress());
        } else {
            iotModbusServerConfig.getMiiServer().sender().closeValve(iotVariableVo.getSerialNum()-1,iotDeviceVo.getSn(), iotDeviceVo.getSlaveAddress());
        }
//        iotModbusServerConfig.getMiiServer().sender().testIot(valveParams.getSn(), valveParams.getSlaveAddress());
        return AjaxResult.success(true);
    }


    @PutMapping("/editValve")
    public AjaxResult editValve(@Validated @RequestBody ValveParams valveParams) {
        iIotModbusService.editValve(valveParams);
        return AjaxResult.success(true);
    }

    @GetMapping("/queryMap")
    public AjaxResult queryMap() {
        Map<String,Object> map = new HashMap<>();
        map.put("CacheUtils",CacheUtils.map);
        map.put("CacheShebeiUtils", CacheShebeiUtils.map);
        return AjaxResult.success(map);
    }

}
